<?php
/**
 * Module used for database connection
 *
 * @author André Silva
 */
class Connection implements IModule {
	private static $dsn;
	private static $username;
	private static $password;
	private static $attributes;
	private static $active;
	private static $schema;
	private static $pdo;

	public static function start($args) {
		import('cerberus.interface.ISchema');
		import('cerberus.data.Model');
		import('cerberus.data.Criteria');
		
		self::$dsn = $args['dsn'];
		self::$username = $args['username'];
		self::$password = $args['password'];
		if (isset ($args['attributes'])) {
			self::$attributes = $args['attributes'];
		}

		self::open();

		switch (self::getDriver()) {
			case 'mysql':
				import('cerberus.data.schema.MysqlSchema');
				self::$schema = new MysqlSchema();
				break;
			default:
				throw new Exception('Database driver not supported');
				break;
		}
	}

	public static function finish() {
		self::close();
	}

	/**
	 * Open database connection
	 */
	public static function open() {
		if (self::$pdo === null) {
			self::$pdo = new PDO(self::$dsn, self::$username, self::$password, self::$attributes);
			self::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			self::$active = true;
		}
	}

	/**
	 * Close database connection
	 */
	public static function close() {
		self::$pdo = null;
		self::$active = false;
	}

	/**
	 * Get a connection attribute value
	 * @param string $name
	 * @return mixed
	 */
	public static function getAttribute($name) {
		if (self::$active) {
			return self::$pdo->getAttribute($name);
		} else {
			throw new Exception('Can\'t get connection attribute: Connection inactive.');
		}
	}

	/**
	 * Set connection attribute value
	 * @param string $name
	 * @param mixed $value
	 */
	public static function setAttribute($name, $value) {
		if (self::$active) {
			self::$pdo->setAttribute($name, $value);
		} else {
			self::$attributes[$name] = $value;
		}
	}

	/**
	 * Get the connectino status
	 * @return boolean
	 */
	public static function getConnectionStatus() {
		return self::getAttribute(PDO::ATTR_CONNECTION_STATUS);
	}

	/**
	 * Get a list of available drivers for database connection
	 * @return array
	 */
	public static function getAvailableDrivers() {
		return PDO::getAvailableDrivers();
	}

	/**
	 * Check if the connection is active
	 * @return boolean
	 */
	public static function isActive() {
		return self::$active;
	}

	/**
	 * Change the connection status
	 * @param boolean $value
	 */
	public static function setActive($value) {
		if ($value !== self::$active) {
			if ($value) {
				self::open();
			}
			else {
				self::close();
			}
		}
	}

	/**
	 * Get the current connection driver name
	 * @return string
	 */
	public static function getDriver() {
		return self::getAttribute(PDO::ATTR_DRIVER_NAME);
	}

	/**
	 * Get the PDO instance
	 * @return PDO
	 */
	public static function &getInstance() {
		return self::$pdo;
	}

	/**
	 * Get the schema class for the current driver
	 * @return ISchema
	 */
	public static function getSchema() {
		return self::$schema;
	}
}
?>
